package algorithm;

import java.util.ArrayList;
import java.util.Map.Entry;

public class TreeNode<T> {
    public ArrayList<TreeNode> subNodes;
    public ArrayList<String> labels;
    public T root;
    public int subCount;

    public TreeNode(T root){
	this.root = root;
	subNodes = new ArrayList<TreeNode>();
	labels = new ArrayList<String>();
    }


    public  ArrayList<TreeNode> getSubNodes(){

	return subNodes;
    }


    public ArrayList<String> getLabels(){
	return labels;
    }



    /**
     * Add a subtreee to the main trunk
     * @param label set the label of the CONNECTION
     * @param subnode link to indicated subnode
     */
    public void addNode(String label, TreeNode subnode){
	subNodes.add(subnode);
	labels.add(label);
    }
    public ArrayList<String> ToStringRaw(String parent){
	ArrayList<String> ret = new ArrayList<String>();
	ret.add(parent + ": " +(String)(root.toString()));
	int i = 0;
	for(TreeNode t : subNodes)
	{
	    
	    ArrayList<String> stringSet = t.ToStringRaw(labels.get(i));
	    for(String s : stringSet){
		s = "\t" + s;
		ret.add(s);
	    }
	    i++;
	}


	return ret;
    }
    public String toString(){

	//String output = root.toString();
	//System.out.println(subNodes.size());
	//String output = "";
	//if(subNodes.isEmpty()){
	//    		output =  " " + root.toString()+ "\n";
	//}
	//output += root.toString();
	//System.out.println("new first " +  output);
	//    	for(TreeNode tn : subNodes ){
	//    		//output += root.toString();
	//    		output +=   "\t" + tn.toString()+  " ";
	//    		//output.replaceFirst("\n", "");
	//    		
	//    	}
	//System.out.println("new second" +  output);
	String output = "";
	for(String c : ToStringRaw("root")){
	    output = output + c + "\n";
	    System.out.println(c);
	}

	return output;
    }

}
